package me.zhengjie.pm.projectissue.domain;

import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import lombok.Data;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import me.zhengjie.pm.issuerelated.domain.IssueRelated;
import me.zhengjie.pm.projectbase.domain.ProjectBase;
import me.zhengjie.pm.projectstage.domain.ProjectStage;
import org.hibernate.annotations.ColumnTransformer;

import javax.persistence.*;
import javax.validation.constraints.*;
import java.sql.Timestamp;
import java.io.Serializable;
import java.util.List;

/**
* @author yuxj
* @date 2020-05-02
*/
@Entity
@Data
@Table(name="project_issue")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class ProjectIssue implements Serializable {

    /** ID */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Integer id;

    /** 问题发生舰船 */
    @Column(name = "ship")
//    @ColumnTransformer(
//            read = "CAST(AES_DECRYPT(UNHEX(ship), 'rjb') as char(128))",
//            write = "HEX(AES_ENCRYPT(?, 'rjb'))"
//    )
    private String ship;

    /** 问题舰艇型号 */
    @Column(name = "ship_type")
//    @ColumnTransformer(
//            read = "CAST(AES_DECRYPT(UNHEX(ship_type), 'rjb') as char(128))",
//            write = "HEX(AES_ENCRYPT(?, 'rjb'))"
//    )
    private String shipType;

    /** 问题发生项目 */
    @Column(name = "project_id",nullable = false)
    @NotNull
    private Integer projectId;

    @ManyToOne
    @JoinColumn(name = "project_id",insertable = false, updatable = false, nullable=true)
    private ProjectBase projectBase;

    /** 问题发生阶段ID */
    @Column(name = "project_stage_id")
    private Integer projectStageId;

    @ManyToOne
    @JoinColumn(name = "project_stage_id",insertable = false, updatable = false, nullable=true)
    private ProjectStage projectStage;

    /** 问题软件标识 */
    @Column(name = "issue_soft_id")
    private Integer issueSoftId;

    /** 问题软件版本号 */
    @Column(name = "issue_soft_version")
    private String issueSoftVersion;

    /** 问题名称 */
    @Column(name = "issue_name",nullable = false)
    @NotBlank
//    @ColumnTransformer(
//            read = "CAST(AES_DECRYPT(UNHEX(issue_name), 'rjb') as char(128))",
//            write = "HEX(AES_ENCRYPT(?, 'rjb'))"
//    )
    private String issueName;

    /** 问题描述 */
    @Column(name = "issue_desc",nullable = false)
    @NotBlank
//    @ColumnTransformer(
//            read = "CAST(AES_DECRYPT(UNHEX(issue_desc), 'rjb') as char(128))",
//            write = "HEX(AES_ENCRYPT(?, 'rjb'))"
//    )
    private String issueDesc;

    /** 问题意见答复 */
    @Column(name = "issue_reply")
//    @ColumnTransformer(
//            read = "CAST(AES_DECRYPT(UNHEX(issue_reply), 'rjb') as char(128))",
//            write = "HEX(AES_ENCRYPT(?, 'rjb'))"
//    )
    private String issueReply;

    /** 问题种类 */
    @Column(name = "issue_type",nullable = false)
    @NotBlank
    private String issueType;

    /** 严重程度 */
    @Column(name = "issue_severity",nullable = false)
    @NotBlank
    private String issueSeverity;

    /** 问题优先级 */
    @Column(name = "priority_level",nullable = false)
    @NotBlank
    private String priorityLevel;

    /** 通报时间 */
    @Column(name = "aviso_time")
    private Timestamp avisoTime;

    /** 通报机关 */
    @Column(name = "aviso_name")
    private String avisoName;

    /** 问题平台 */
    @Column(name = "issue_plat",nullable = false)
    @NotBlank
    private String issuePlat;

    /** 质量问题来源 */
    @Column(name = "issue_source_type",nullable = false)
    @NotBlank
    private String issueSourceType;

    /** 质量问题来源地 */
    @Column(name = "issue_source_locate",nullable = false)
    @NotBlank
    private String issueSourceLocate;

    /** 问题分析 */
    @Column(name = "analyse")
//    @ColumnTransformer(
//            read = "CAST(AES_DECRYPT(UNHEX(analyse), 'rjb') as char(128))",
//            write = "HEX(AES_ENCRYPT(?, 'rjb'))"
//    )
    private String analyse;

    /** 问题性质 */
    @Column(name = "issue_quality",nullable = false)
    @NotBlank
    private String issueQuality;

    /** 问题发现人 */
    @Column(name = "issue_finder_id")
    private Integer issueFinderId;

    /** 问题发现时间 */
    @Column(name = "issue_discovery_time")
    private Timestamp issueDiscoveryTime;

    /** 计划完成时间 */
    @Column(name = "plan_finish_time")
    private Timestamp planFinishTime;

    /** 实际解决时间 */
    @Column(name = "issue_finish_time")
    private Timestamp issueFinishTime;

    /** 处理完成状态 */
    @Column(name = "issue_finish_state")
    private String issueFinishState;

    /** 问题所属功能域 */
    @Column(name = "issue_function_area")
    private String issueFunctionArea;

    /** 完成情况说明 */
    @Column(name = "issue_finish_remark")
    private String issueFinishRemark;

    /** 项目组提出问题时间 */
    @Column(name = "project_team_raise_questions_time")
    private Timestamp projectTeamRaiseQuestionsTime;

    /** 项目组要求完成时间 */
    @Column(name = "project_team_require_finish_time")
    private Timestamp projectTeamRequireFinishTime;

    /** 项目组确定需求时间 */
    @Column(name = "project_team_confirm_demand_time")
    private Timestamp projectTeamConfirmDemandTime;

    /** 需求是否明确 */
    @Column(name = "is_demand_clear")
    private String isDemandClear;

    /** 项目组问题修改建议 */
    @Column(name = "project_team_suggest")
//    @ColumnTransformer(
//            read = "CAST(AES_DECRYPT(UNHEX(project_team_suggest), 'rjb') as char(128))",
//            write = "HEX(AES_ENCRYPT(?, 'rjb'))"
//    )
    private String projectTeamSuggest;

    /** 功能域子模块名称 */
    @Column(name = "module_name")
    private String moduleName;

    /** 域负责人 */
    @Column(name = "domain_head_person")
    private Integer domainHeadPerson;

    /** 问题确认情况 */
    @Column(name = "issue_comfirm_type")
    private String issueComfirmType;

    /** 反馈意见 */
    @Column(name = "feedback")
//    @ColumnTransformer(
//            read = "CAST(AES_DECRYPT(UNHEX(feedback), 'rjb') as char(128))",
//            write = "HEX(AES_ENCRYPT(?, 'rjb'))"
//    )
    private String feedback;

    /** 问题分析及解决措施 */
    @Column(name = "analyse_solutions")
//    @ColumnTransformer(
//            read = "CAST(AES_DECRYPT(UNHEX(analyse_solutions), 'rjb') as char(128))",
//            write = "HEX(AES_ENCRYPT(?, 'rjb'))"
//    )
    private String analyseSolutions;

    /** 问题修改情况 */
    @Column(name = "modification")
//    @ColumnTransformer(
//            read = "CAST(AES_DECRYPT(UNHEX(modification), 'rjb') as char(128))",
//            write = "HEX(AES_ENCRYPT(?, 'rjb'))"
//    )
    private String modification;

    /** 是否发布 */
    @Column(name = "is_release")
    private String isRelease;

    /** 集成情况 */
    @Column(name = "integration")
//    @ColumnTransformer(
//            read = "CAST(AES_DECRYPT(UNHEX(integration), 'rjb') as char(128))",
//            write = "HEX(AES_ENCRYPT(?, 'rjb'))"
//    )
    private String integration;

    /** 验证情况 */
    @Column(name = "verification")
//    @ColumnTransformer(
//            read = "CAST(AES_DECRYPT(UNHEX(verification), 'rjb') as char(128))",
//            write = "HEX(AES_ENCRYPT(?, 'rjb'))"
//    )
    private String verification;

    /** 集成验证情况 */
    @Column(name = "integration_verification")
//    @ColumnTransformer(
//            read = "CAST(AES_DECRYPT(UNHEX(integration_verification), 'rjb') as char(128))",
//            write = "HEX(AES_ENCRYPT(?, 'rjb'))"
//    )
    private String integrationVerification;

    /** 测试方 */
    @Column(name = "test_team")
    private String testTeam;

    /** 测试人员 */
    @Column(name = "testers")
    private String testers;

    /** 测试时间 */
    @Column(name = "test_time")
    private Timestamp testTime;

    /** 测试项标志 */
    @Column(name = "test_mark")
    private String testMark;

    /** 被测试问题类别 */
    @Column(name = "test_type")
    private String testType;

    /** 测试情况说明 */
    @Column(name = "test_describe")
//    @ColumnTransformer(
//            read = "CAST(AES_DECRYPT(UNHEX(test_describe), 'rjb') as char(128))",
//            write = "HEX(AES_ENCRYPT(?, 'rjb'))"
//    )
    private String testDescribe;

    /** 问题归零完成状态 */
    @Column(name = "zero_state")
    private String zeroState;

    /** 质量问题通报时间 */
    @Column(name = "zero_time")
    private Timestamp zeroTime;

    /** 是否质量处问题库问题 */
    @Column(name = "in_quality_library")
    private String inQualityLibrary;

    /** 处理后软件版本 */
    @Column(name = "after_edition")
    private String afterEdition;

    /** 问题所属变更申请单号 */
    @Column(name = "change_num")
    private String changeNum;

    /** 问题所入库申请单号 */
    @Column(name = "in_storage_num")
    private String inStorageNum;

    /** 本问题引发的关联型号列表，由阶段进度汇报表的issue_id字段为外键 */
    @OneToMany(targetEntity =IssueRelated.class, cascade=CascadeType.REFRESH)
    @JoinColumn(name="issue_id")
    List<IssueRelated> issueRelatedList;

    public void copy(ProjectIssue source){
        BeanUtil.copyProperties(source,this, CopyOptions.create().setIgnoreNullValue(true));
    }
}